package vn.edu.saigontech.ncms.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import vn.edu.saigontech.ncms.service.RoleService;
import vn.edu.saigontech.ncms.service.SiteService;
import vn.edu.saigontech.ncms.util.Global;
import vn.edu.saigontech.ncms.util.HibernateUtil;

public class ContextLoaderListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent event) {
		HibernateUtil.getCurrentSession().beginTransaction();
		try {
			ServletContext context = event.getServletContext();

			Global.PATH_WEB = context.getRealPath("/");
			Global.INSTANCE.load();

			context.setAttribute(Global.KEY, Global.INSTANCE);
			context.setAttribute("sites", SiteService.INSTANCE.findAll());
			context.setAttribute("roles", RoleService.INSTANCE.findAll());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		HibernateUtil.getCurrentSession().close();
	}

	public void contextDestroyed(ServletContextEvent event) {
	}
}